package com.example.usedcardealer;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CarsDAO {
    private Context context;
    private MyDBHelper dbHelper;
    private SQLiteDatabase db;

    //构造函数
    public CarsDAO(Context context) {
        this.context = context;
    }

    //打开数据库
    public void open() throws SQLiteException {
        dbHelper = new MyDBHelper(context);
        try {
            db = dbHelper.getWritableDatabase();
        } catch (SQLiteException ex) {
            db = dbHelper.getReadableDatabase();
        }
    }

    //关闭数据库
    public void close() {
        if (db != null) {
            db.close();
            db = null;
        }
    }


    //添加信息
    public long addCars(Car a) {
        // 创建ContentValues对象
        ContentValues values = new ContentValues();
        // 向该对象中插入键值对
        values.put("cjh", a.cjh);
        values.put("pp", a.pp);
        values.put("xh", a.xh);
        values.put("lx", a.lx);

        // 调用insert()方法将数据插入到数据库当中
        return db.insert("tb_Cars", null, values);
    }

    //删除指定信息
    public int deletCars(Car a) {
        return db.delete("tb_Cars", "cjh=?", new String[]{String.valueOf(a.cjh)});
    }

    //修改指定信息
    public int updateCars(Car a) {
        ContentValues value = new ContentValues();
        value.put("pp", a.pp);
        value.put("xh", a.xh);
        value.put("lx", a.lx);
        return db.update("tb_Cars", value, "cjh=?", new String[]{String.valueOf(a.cjh)});
    }

    //根据车架号查找车辆
    @SuppressLint("Range")
    public Car getCars(String cjh) {
        //查询学生
        Cursor cursor = db.query("tb_Cars", null, "cjh=?", new String[]{cjh}, null, null, null);
        Car a = new Car();
        while (cursor.moveToNext()) {
            a.cjh = cursor.getString(cursor.getColumnIndex("cjh"));
            a.pp = cursor.getString(cursor.getColumnIndex("pp"));
            a.xh = cursor.getString(cursor.getColumnIndex("xh"));
            a.lx = cursor.getString(cursor.getColumnIndex("lx"));

        }
        return a;
    }

    //查找所有信息
    @SuppressLint("Range")
    public ArrayList<Map<String, Object>> getAllcars() {
        ArrayList<Map<String, Object>> listCars = new ArrayList<Map<String, Object>>();
        Cursor cursor = db.query("tb_Cars", null, null, null, null, null,null);

        int resultCounts = cursor.getCount();  //记录数
        if (resultCounts == 0 ) {
            return null;
        } else {
            while (cursor.moveToNext()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("cjh", cursor.getString(cursor.getColumnIndex("cjh")));
                map.put("pp", cursor.getString(cursor.getColumnIndex("pp")));
                map.put("xh", cursor.getString(cursor.getColumnIndex("xh")));
                map.put("lx", cursor.getString(cursor.getColumnIndex("lx")));

                listCars.add(map);
            }
            return listCars;
        }
    }
}
